package com.fyzk.activity.lottery.shuzicai;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.down_numhistory.NumHistoryPrizeView;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallClickListener;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallItemClickLitener;
import com.fyzk.activity.myview.BallPopFrameLayout;
import com.fyzk.activity.myview.BallScrollView;
import com.fyzk.activity.myview.DrawBallFrameLayout;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.activity.trend.PL5TrendActivity;
import com.fyzk.bean.PrizeHistoryItem;
import com.tyjc.lottery.R;
import com.fyzk.random.RandomFactory;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;

import java.util.ArrayList;
import java.util.List;


public class PL5Activity extends LotteryBaseActivity implements
		BallClickListener, BallItemClickLitener {

	private BallPopFrameLayout moveQiu;

	private TopTitleView topView;

	private DrawBallFrameLayout ball_linear;
	private BallScrollView ball_scroll;

	SelectBallView ballView_firstLine;
	SelectBallView ballView_twoline;
	SelectBallView ballView_threeline;
	SelectBallView ballView_fourline;
	SelectBallView ballView_fiveline;

	List<BallInfo> balls_firstLine = new ArrayList<BallInfo>();
	List<BallInfo> balls_twoLine = new ArrayList<BallInfo>();
	List<BallInfo> balls_threeLine = new ArrayList<BallInfo>();
	List<BallInfo> balls_fourLine = new ArrayList<BallInfo>();
	List<BallInfo> balls_fiveLine = new ArrayList<BallInfo>();

	private TextView bottom_zhushu;

	private TextView bottom_money;
	
	//新加
	private LinearLayout ll_history;
	private  LinearLayout shake_random_ll;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dj_activity_pl5);
		initBallView();
//		ballClick();
	}

	@Override
	public TopTitleView getTopView() {
		return topView;
	}
	
	private void initBallView() {
		TouZhuHelper.getInstance().setBallClickListener(this);
		TouZhuHelper.getInstance().setBallItemClickLitener(this);

		topView = (TopTitleView) findViewById(R.id.topTitle);
		topView.setRightVisible(View.VISIBLE);
		topView.setRightImg(R.drawable.downmenu_icon);
		topView.setOnTitleClickListener(this);
		moveQiu = (BallPopFrameLayout) findViewById(R.id.pl5_fl_moveqiu);
		ball_linear = (DrawBallFrameLayout) findViewById(R.id.pl5_drawball_framelayout);
		ball_scroll = (BallScrollView) findViewById(R.id.pl5_ballscroll);
		ball_linear.setScr(ball_scroll);
		ball_linear.setFl_moveqiu(moveQiu);
		ball_linear.setUpOrDownListener(this);
		ballView_firstLine = (SelectBallView) findViewById(R.id.firstLine);
		ballView_twoline = (SelectBallView) findViewById(R.id.twoLine);
		ballView_threeline = (SelectBallView) findViewById(R.id.thressLine);
		ballView_fourline = (SelectBallView) findViewById(R.id.fourLine);
		ballView_fiveline = (SelectBallView) findViewById(R.id.fiveline);
		bottom_zhushu = (TextView) findViewById(R.id.pl5_bottom_tv_zhushu);
		bottom_money = (TextView) findViewById(R.id.pl5_bottom_tv_money);

		shake_random_ll = (LinearLayout)findViewById(R.id.shake_random_ll);
		shake_random_ll.setOnClickListener(this);

		//新加
		ll_history = (LinearLayout) findViewById(R.id.pl5_ball_history);

		initArrayInfos();

		initBottomView();
	}

	@Override
	protected void initArrayInfos() {
		if (arr_infos.size() == 5 ) {
			balls_firstLine = arr_infos.get(0);
			balls_twoLine = arr_infos.get(1);
			balls_threeLine = arr_infos.get(2);
			balls_fourLine = arr_infos.get(3);
			balls_fiveLine = arr_infos.get(4);
		}else{
			arr_infos.clear();
			initBallList(balls_firstLine, 10,0,true);
			initBallList(balls_twoLine, 10,0,true);
			initBallList(balls_threeLine, 10,0,true);
			initBallList(balls_fourLine, 10,0,true);
			initBallList(balls_fiveLine, 10,0,true);
			arr_infos.add(balls_firstLine);
			arr_infos.add(balls_twoLine);
			arr_infos.add(balls_threeLine);
			arr_infos.add(balls_fourLine);
			arr_infos.add(balls_fiveLine);
		}

		ballView_firstLine.setBallInfoList(balls_firstLine);
		ballView_twoline.setBallInfoList(balls_twoLine);
		ballView_threeline.setBallInfoList(balls_threeLine);
		ballView_fourline.setBallInfoList(balls_fourLine);
		ballView_fiveline.setBallInfoList(balls_fiveLine);
	}


	@Override
	public void ballClick(WeiZhi info) {
		// TODO Auto-generated method stub
		WeiZhi w = info;
		// TODO Auto-generated method stub
		w.setType(0);
		int left = w.getLeft() - 5;
		int right = w.getRight() + 5;
		int top = (int) ((ball_linear.getY1() - w.getPointY() + w.getTop())
				- (w.getBottom() - w.getTop()) - 10);
		int bottom = (int) (ball_linear.getY1() - w.getPointY() + w.getTop());
		Rect rect = new Rect(left, top, right, bottom);
		Log.e("FramLayout   画球区", ".............  gettype:" + w.getType());
		moveQiu.setRect(rect, w.getNum(), w.getType(), w.isShowMoveBall);
	}
	int money;
	@Override
	public void ballClick() {
		// TODO Auto-generated method stub
		int zhushu = getZhushu();
		money = zhushu * 2;
		bottom_zhushu.setText(zhushu + "注 ");
		bottom_money.setText("共" + money + "米");

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()){
			case R.id.shake_random_ll:
				randomBallNum();
				break;
		}
	}

	@Override
	public boolean touZhu() {
		// TODO Auto-generated method stub
		int zhuShu = getZhushu();
		if (zhuShu > 0) {
//			if(zhuShu>maxZhuShu){
//				AppToast.getToast().show("单注投注彩金不能大于100000!");
//				return false;
//			}
			int lotteryId = zhuShu > 1 ? LotteryIDUtils.PL5_LOTTERY_COMMON_FU_ID_INT
					: LotteryIDUtils.PL5_LOTTERY_COMMON_DAN_ID_INT;
			touZhu1(lotteryId, zhuShu);
			return true;
		}else{
			return false;
		}
	}

	private int getZhushu() {
		if (null == arr_infos) {
			arr_infos = new ArrayList<List<BallInfo>>();
			return 0;
		}
		int zhushu = 1;
		for (int i = 0; i < arr_infos.size(); i++) {
			int j = 0;
			List<BallInfo> infos = arr_infos.get(i);
			for (BallInfo info : infos) {
				if (info.isSelected()) {
					j++;
				}
			}
			zhushu *= j;
		}
		return zhushu;
	}
	
	@Override
	public void onLeftClick() {
		// TODO Auto-generated method stub
		onBackClick();
	}
	
	private void onBackClick(){
		
//		if(AppLotteryCodeListManager.getInstance().getList().size()>0){
//			
//		};
		finish();
	}

	@Override
	public int getLotteryId() {
		// TODO Auto-generated method stub
		return LotteryIDUtils.PL5_LOTTERY_ID_INT;
	}

	@Override
	public void randomBallNum() {
		// TODO Auto-generated method stub
		int first = RandomFactory.getRandom(LotteryIDUtils.PL5_LOTTERY_ID_INT).randomDanHang(1)[0];
		int two = RandomFactory.getRandom(LotteryIDUtils.PL5_LOTTERY_ID_INT).randomDanHang(1)[0];
		int thire = RandomFactory.getRandom(LotteryIDUtils.PL5_LOTTERY_ID_INT).randomDanHang(1)[0];
		int four = RandomFactory.getRandom(LotteryIDUtils.PL5_LOTTERY_ID_INT).randomDanHang(1)[0];
		int five = RandomFactory.getRandom(LotteryIDUtils.PL5_LOTTERY_ID_INT).randomDanHang(1)[0];
		
		initBallList(balls_firstLine, 10,0,true);
		initBallList(balls_twoLine, 10,0,true);
		initBallList(balls_threeLine, 10,0,true);
		initBallList(balls_fourLine, 10,0,true);
		initBallList(balls_fiveLine, 10,0,true);
		
		balls_firstLine.get(first).setSelected(true);
		balls_twoLine.get(two).setSelected(true);
		balls_threeLine.get(thire).setSelected(true);
		balls_fourLine.get(four).setSelected(true);
		balls_fiveLine.get(five).setSelected(true);
		
		ballView_firstLine.invalidate();
		ballView_twoline.invalidate();
		ballView_threeline.invalidate();
		ballView_fourline.invalidate();
		ballView_fiveline.invalidate();
		
		ballClick();
	}

	@Override
	protected void addHistoryView(ArrayList<PrizeHistoryItem> historyItems, boolean isWaitting) {
		ll_history.removeAllViews();
		ll_history.addView(new NumHistoryPrizeView(getContext(), historyItems).getView());
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		bottom_zhushu.setText(0 + "注 ");
		bottom_money.setText("共" + 0 + "米");
		
		initBallList(balls_firstLine, 10, 0,true);
		initBallList(balls_twoLine, 10, 0,true);
		initBallList(balls_threeLine, 10, 0,true);
		initBallList(balls_fourLine, 10, 0,true);
		initBallList(balls_fiveLine, 10, 0,true);
		
		ballView_firstLine.invalidate();
		ballView_twoline.invalidate();
		ballView_threeline.invalidate();
		ballView_fourline.invalidate();
		ballView_fiveline.invalidate();
	}

	@Override
	public String getHelpUrl() {
		// TODO Auto-generated method stub
		return HelpUrlConstants.Help_PL5;
	}

	@Override
	public String getHelpTitle() {
		// TODO Auto-generated method stub
		return getString(R.string.help_pl5);
	}

	@Override
	public void onRightClick() {
		// TODO Auto-generated method stub
		super.onRightClick();
		toHelp();
	}

	@Override
	protected void popupClickItem(int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void addMissValue() {
		super.addMissValue();
		try {
			if (missInfo != null) {
				ballView_firstLine.setMissValues(missInfo.missValue("2003MV_ZX").get(0));
				ballView_twoline.setMissValues(missInfo.missValue("2003MV_ZX").get(1));
				ballView_threeline.setMissValues(missInfo.missValue("2003MV_ZX").get(2));
				ballView_fourline.setMissValues(missInfo.missValue("2003MV_ZX").get(3));
				ballView_fiveline.setMissValues(missInfo.missValue("2003MV_ZX").get(4));

			}
		} catch (Exception e) {

		}


	}

	@Override
	public Intent getTrendIntent() {
		Intent intent = new Intent(getContext(), PL5TrendActivity.class);
		intent.putExtra(IntentParamUtils.LOTTERYID, getLotteryId());
		return intent;
	}
}
